import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

class Student {
    private String studentId;
    private String name;

    public Student(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
    }

    public String getStudentId() {
        return studentId;
    }

    public String getName() {
        return name;
    }
}

class Course {
    private String courseName;

    public Course(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseName() {
        return courseName;
    }
}

class Grade {
    private Student student;
    private Course course;
    private double score;

    public Grade(Student student, Course course, double score) {
        this.student = student;
        this.course = course;
        this.score = score;
    }

    public Student getStudent() {
        return student;
    }

    public Course getCourse() {
        return course;
    }

    public double getScore() {
        return score;
    }
}

class GradeManager {
    private Map<String, Student> students = new HashMap<>();
    private Map<String, Course> courses = new HashMap<>();
    private List<Grade> grades = new ArrayList<>();

    public boolean addStudent(String studentId, String name) {
        if (students.containsKey(studentId)) {
            return false;
        }
        students.put(studentId, new Student(studentId, name));
        return true;
    }

    public boolean addCourse(String courseName) {
        if (courses.containsKey(courseName)) {
            return false;
        }
        courses.put(courseName, new Course(courseName));
        return true;
    }

    public boolean addGrade(String studentId, String courseName, double score) {
        if (!students.containsKey(studentId)) {
            return false;
        }
        if (!courses.containsKey(courseName)) {
            return false;
        }
        if (score < 0 || score > 100) {
            return false;
        }
        for (Grade grade : grades) {
            if (grade.getStudent().getStudentId().equals(studentId) &&
                grade.getCourse().getCourseName().equals(courseName)) {
                return false;
            }
        }
        grades.add(new Grade(
            students.get(studentId),
            courses.get(courseName),
            score
        ));
        return true;
    }

    public List<Grade> queryGradesByStudentId(String studentId) {
        List<Grade> result = new ArrayList<>();
        for (Grade grade : grades) {
            if (grade.getStudent().getStudentId().equals(studentId)) {
                result.add(grade);
            }
        }
        return result;
    }

    public List<Grade> queryGradesByName(String name) {
        List<Grade> result = new ArrayList<>();
        for (Grade grade : grades) {
            if (grade.getStudent().getName().equals(name)) {
                result.add(grade);
            }
        }
        return result;
    }

    public List<Grade> queryGradesByCourse(String courseName) {
        List<Grade> result = new ArrayList<>();
        for (Grade grade : grades) {
            if (grade.getCourse().getCourseName().equals(courseName)) {
                result.add(grade);
            }
        }
        return result;
    }

    public Map<String, Double> calculateCourseStatistics(String courseName) {
        List<Grade> courseGrades = queryGradesByCourse(courseName);
        Map<String, Double> statistics = new HashMap<>();
        
        if (courseGrades.isEmpty()) {
            statistics.put("average", 0.0);
            statistics.put("max", 0.0);
            statistics.put("min", 0.0);
            return statistics;
        }
        
        double sum = 0;
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        
        for (Grade grade : courseGrades) {
            double score = grade.getScore();
            sum += score;
            if (score > max) max = score;
            if (score < min) min = score;
        }
        
        statistics.put("average", sum / courseGrades.size());
        statistics.put("max", max);
        statistics.put("min", min);
        
        return statistics;
    }
}

class StudentGradeManager {
    private static Scanner scanner = new Scanner(System.in);
    private static GradeManager manager = new GradeManager();

    public static void main(String[] args) {
        while (true) {
            printMenu();
            int choice = getIntInput("请输入选项: ");
            
            switch (choice) {
                case 1:
                    addGrade();
                    break;
                case 2:
                    queryByStudentId();
                    break;
                case 3:
                    queryByName();
                    break;
                case 4:
                    queryByCourse();
                    break;
                case 5:
                    showCourseStatistics();
                    break;
                case 6:
                    System.out.println("退出系统，再见！");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private static void printMenu() {
        System.out.println("\n===== 学生成绩管理系统 =====");
        System.out.println("1. 录入学生成绩");
        System.out.println("2. 按学号查询成绩");
        System.out.println("3. 按姓名查询成绩");
        System.out.println("4. 按课程查询成绩");
        System.out.println("5. 查看课程统计信息");
        System.out.println("6. 退出系统");
        System.out.println("===========================");
    }

    private static void addGrade() {
        String studentId = getStringInput("请输入学号: ");
        String name = getStringInput("请输入姓名: ");
        String courseName = getStringInput("请输入课程名称: ");
        double score = getDoubleInput("请输入成绩(0-100): ");
        
        if (!manager.addStudent(studentId, name)) {
            System.out.println("添加学生失败，学号已存在！");
        }
        
        if (!manager.addCourse(courseName)) {
            System.out.println("添加课程失败，课程已存在！");
        }
        
        if (manager.addGrade(studentId, courseName, score)) {
            System.out.println("成绩录入成功！");
        } else {
            System.out.println("成绩录入失败，可能原因：学号不存在、课程不存在、成绩超出范围或已有该学生该课程的成绩！");
        }
    }

    private static void queryByStudentId() {
        String studentId = getStringInput("请输入学号: ");
        List<Grade> grades = manager.queryGradesByStudentId(studentId);
        
        if (grades.isEmpty()) {
            System.out.println("未找到该学生的成绩记录！");
        } else {
            System.out.println("查询结果：");
            for (Grade grade : grades) {
                System.out.printf("%s %s %s: %.1f\n", 
                    grade.getStudent().getStudentId(),
                    grade.getStudent().getName(),
                    grade.getCourse().getCourseName(),
                    grade.getScore());
            }
        }
    }

    private static void queryByName() {
        String name = getStringInput("请输入姓名: ");
        List<Grade> grades = manager.queryGradesByName(name);
        
        if (grades.isEmpty()) {
            System.out.println("未找到该学生的成绩记录！");
        } else {
            System.out.println("查询结果：");
            for (Grade grade : grades) {
                System.out.printf("%s %s %s: %.1f\n", 
                    grade.getStudent().getStudentId(),
                    grade.getStudent().getName(),
                    grade.getCourse().getCourseName(),
                    grade.getScore());
            }
        }
    }

    private static void queryByCourse() {
        String courseName = getStringInput("请输入课程名称: ");
        List<Grade> grades = manager.queryGradesByCourse(courseName);
        
        if (grades.isEmpty()) {
            System.out.println("未找到该课程的成绩记录！");
        } else {
            System.out.println("查询结果：");
            for (Grade grade : grades) {
                System.out.printf("%s %s %s: %.1f\n", 
                    grade.getStudent().getStudentId(),
                    grade.getStudent().getName(),
                    grade.getCourse().getCourseName(),
                    grade.getScore());
            }
        }
    }

    private static void showCourseStatistics() {
        String courseName = getStringInput("请输入课程名称: ");
        Map<String, Double> stats = manager.calculateCourseStatistics(courseName);
        
        System.out.printf("%s 课程统计信息:\n", courseName);
        System.out.printf("平均分: %.2f\n", stats.get("average"));
        System.out.printf("最高分: %.2f\n", stats.get("max"));
        System.out.printf("最低分: %.2f\n", stats.get("min"));
    }

    private static String getStringInput(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    private static int getIntInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                int input = Integer.parseInt(scanner.nextLine());
                return input;
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入一个整数！");
            }
        }
    }

    private static double getDoubleInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                double input = Double.parseDouble(scanner.nextLine());
                return input;
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入一个数字！");
            }
        }
    }
}    







、